//Arduino 1.8.4
//This code is used for the stretching device 
//use the serial monitor to select the desired strain by entering: G
//enter D :to select the speed
//enter B : to start cycling test 

int SSS = 0;
long  S;
long D;
int D11 = D;
long P1;
int P2;

long P;
long RGB = -0.2005;
float M ;
int MM ;
long o = 0;
int GGGG;
long L = -0.2005;


int analogPin= 1;
int raw= 0;
int Vin= 5;
float Vout= 0;
float R1= 1000;
float R2= 0;
float buffer= 0;
long OldMillis=0;
float Delay = 1000;
void setup() {

Serial.begin(9600);
  pinMode(9 , OUTPUT);     //pul
    pinMode(8 , OUTPUT);   //dir
    
      pinMode(A0 , INPUT);
      pinMode(7 , INPUT);

            
D = 1000;

P1 = 0;
P = 0;

 digitalWrite(8 , LOW); 


state321:

   digitalWrite(9 , HIGH);
   delayMicroseconds(1000);
   digitalWrite(9 , LOW);
      delayMicroseconds(1000);

   if(digitalRead(7) == HIGH ){ goto state123; }else{ goto state321;}

      

state123:

 digitalWrite(8 , HIGH);  

 for (int i=0; i <=4500 ; i++){   /////////// ( 1 ) from here we can change the initial number of steps of the stepper motor


   digitalWrite(9 , HIGH);
   delayMicroseconds(1000);
   digitalWrite(9 , LOW);
      delayMicroseconds(1000);

 }




}

void loop() {








  if( Serial.available() )      
  {
    S = Serial.read();        
  }




if(S == 'D'){ ///// press 'D' in the serial monitor to select the speed 
// S = "";

  Serial.println("please select the speed...");

  state:
  if( Serial.available())      
  {
    D = Serial.parseInt(); 
    Delay=((float)D*1000)/5000;
 // int D1 = D;
      Serial.print(" speed = ");
      Serial.println(D);
       Serial.print(" Delay = ");
      Serial.println(Delay);
  
  }else{goto state;}

}

if(S == 'G'){ ///// press 'G' in the serial monitor to select the strain as a percentage 

 Serial.println("please select Strain...");



   state1:
  if( Serial.available())      
  {
    L = Serial.parseInt(); 
      Serial.print(" Strain = ");
      Serial.println(L);
    
    
  }else{goto state1;}

}

P  = (L+1.078)/0.0028 ;// from here we can change the equation that indicates the relationship between the strain as a percentage and the steps of the stepper motor 



 int DDD = P;
 
 if(S == 'B'){ ///// press 'B' in the serial monitor to select mode B ( Mode B for cyclic test )

 Serial.println("B mode");
 o = 10;
}
 
if(S == 'A'){ ///// press 'A' in the serial monitor to select mode A (mode A for moving to the selected strain )

 Serial.println("A mode");
 o = 0;
 goto abcd1;
}
if( o > 5 ){

  if( L > 1 ){
 
 digitalWrite(8 , LOW);
 
 for (int i=0; i <= P; i--){   /////////// ( 1 )
  
//Arduino ohm meter ( to show the real time values of the resistance during the cyclic test)

if (millis()-OldMillis > Delay){
  
  OldMillis = millis();
  raw= analogRead(analogPin);
  if(raw) 
  {
        buffer= raw * Vin;
        Vout= (buffer)/1024.0;
        buffer= (Vin/Vout) -1;
        R2= R1 * buffer;
        Serial.print("Cycle: ");
        Serial.print(SSS );
        Serial.print("\t");
        Serial.print("R2: ");
        Serial.println(R2);
  
  }

}

   digitalWrite(9 , HIGH);
   delayMicroseconds(D);
   digitalWrite(9 , LOW);
      delayMicroseconds(D);

 }
 
 delay(100); 
 
 digitalWrite(8 , HIGH);
for (int i=P ; i >= 0 ; i++){   /////////// ( 1 )


   digitalWrite(9 , HIGH);
   delayMicroseconds(D);
   digitalWrite(9 , LOW);
      delayMicroseconds(D);

if (millis()-OldMillis > Delay){
  
  OldMillis = millis();
  raw= analogRead(analogPin);
  if(raw) 
  {
        buffer= raw * Vin;
        Vout= (buffer)/1024.0;
        buffer= (Vin/Vout) -1;
        R2= R1 * buffer;
        Serial.print("Cycle: ");
        Serial.print(SSS);
        Serial.print("\t");
        Serial.print("R2: ");
        Serial.println(R2);
  
  }

}

}




 delay(100); 

SSS++;
 Serial.println(SSS);

  goto ZZ;
  
}
}
  


abcd1:
 if( P == P1 ){ goto state4; } 
 
 if( P > P1 ){ digitalWrite(8 , HIGH);
 
 for (int i=P1; i <= P; i--){   /////////// ( 1 )



   digitalWrite(9 , HIGH);
   delayMicroseconds(D);
   digitalWrite(9 , LOW);
      delayMicroseconds(D);


}
 
 
 
 
 }
 
   if( P < P1 ){ 
 digitalWrite(8 , LOW);


for (int i=P; i <= P1; i++){   /////////// ( 1 )


   digitalWrite(9 , HIGH);
   delayMicroseconds(D);
   digitalWrite(9 , LOW);
      delayMicroseconds(D);
}

}

ZZ:
state4:
 P1 = P;
// S =0;



 S =0;

}
